package gui.panels;

import gui.tools.ActionHandler;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import mysql.LocationQueries;
import mysql.PersonQueries;
import objects.Location;
import objects.Person;

public class PersonPanel extends JPanel {

	public Person pcurrentEntry;
	public PersonQueries personQueries;
	public List<Person> pResults;
	public int pnumberOfEntries = 0;
	public int pCurrentEntryIndex = 0;
	
	public LocationQueries locQueries;
	public List<Location> locResults;

	private JLabel pidLabel;
	public JTextField pidText;
	private JLabel firstnameLabel;
	public JTextField firstnameText;
	private JLabel lastnameLabel;
	public JTextField lastnameText;
	private JLabel locationLabel;
	public JTextField locationText;
	public JButton next;
	public JButton previous;
	public JPanel navigatePanel;
	public JTextField index;
	private JPanel displayPanel;
	public JButton add;

	public PersonPanel(ActionHandler ac) {

		getData();
		pidLabel = new JLabel("Person ID:");
		pidText = new JTextField(4);
		firstnameLabel = new JLabel("Firstname:");
		firstnameText = new JTextField(10);
		lastnameLabel = new JLabel("Lastname:");
		lastnameText = new JTextField(10);
		locationLabel = new JLabel("Location ID:");
		locationText = new JTextField(2);
		next = new JButton("Next");
		previous = new JButton("Previous");
		navigatePanel = new JPanel();
		index = new JTextField(2);
		displayPanel = new JPanel();
		add = new JButton("Add new user");

		pidText.setEditable(false);
		firstnameText.setEditable(false);
		lastnameText.setEditable(false);
		locationText.setEditable(false);

		setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

		next.addActionListener(ac);
		previous.addActionListener(ac);
		navigatePanel.setLayout(new BoxLayout(navigatePanel, BoxLayout.X_AXIS));
		navigatePanel.add(previous);
		navigatePanel.add(Box.createHorizontalStrut(10));
		navigatePanel.add(index);
		navigatePanel.add(Box.createHorizontalStrut(10));
		navigatePanel.add(next);
		add(navigatePanel);

		displayPanel.setLayout(new GridLayout(4, 2, 4, 4));
		displayPanel.add(pidLabel);
		pidText.setEditable(false);
		displayPanel.add(pidText);
		displayPanel.add(firstnameLabel);
		displayPanel.add(firstnameText);
		displayPanel.add(lastnameLabel);
		displayPanel.add(lastnameText);
		displayPanel.add(locationLabel);
		displayPanel.add(locationText);
		
		add.addActionListener(ac);
		
		add(displayPanel);
		add(add, BorderLayout.SOUTH);
	}

	public void getData() {
		personQueries = new PersonQueries();
		pResults = personQueries.getAllPeople();
		
		locQueries = new LocationQueries();
		locResults = locQueries.getAllLocations();
	}

}
